#!/usr/bin/perl

################################################################################
# rutouc.pl - Russian to HTML Unicode Converter
# Copyright (C) 2008 Marcus Nutzinger <www.nuuz.at>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
################################################################################

use strict;
use warnings;
use Switch;			# switch statement

# checking for arguments, one input file has to be given
unless (@ARGV == 1) {
	print STDERR "Usage: $0 INPUT\n";
	exit 1;
}

my $file = shift(@ARGV);	# filename
my $infp;			# file pointer

# open input file
unless (open($infp, "<", $file)) {
	print STDERR "*** Unable to open \"$file\" for reading.\n";
	exit 1;
}

my $rutouc = ru_uc_map();	# get mapping of defined characters and unicode

my @chars = (0);
my $char = "";

print "rutouc.pl - Reading from file \"$file\" ...\n\nOutput\n";

while (<$infp>) {		# read file
	@chars = split //;	# get string char by char

	foreach (@chars) {	# check each char
		# sort out punctuation characters
		if (print_line_chars($_)) {
			print $_;
			next;
		}

		$char .= $_;

		# check hash map for unicode sign
		if ($rutouc->{$char}) {
			print "&#$rutouc->{$char};";
			$char = "";
			next;
		}
	}
}

close $infp;
exit 0;

################################################################################

# checks current character for punctuation character and returns 1 in case
sub print_line_chars {
	switch ($_) {
		case /[[:punct:]]/ { return 1 };
		case /[[:space:]]/ { return 1 };
	}

	return 0;
}

# opens the given filename in the given mode and returns a filepointer
sub open_file {
	my $file = shift;
	my $mode = shift;
	my $fp;


	return $fp;
}

# returns a hash with character <-> unicode mapping
sub ru_uc_map {
	my $map = {
	A   => 1040, a   => 1072,
	B   => 1041, b   => 1073,
	W   => 1042, w   => 1074,
	G   => 1043, g   => 1075,
	D   => 1044, d   => 1076,
	E   => 1045, e   => 1077,
	vJO => 1025, vjo => 1105,
	vZH => 1046, vzh => 1078,
	vSS => 1047, vss => 1079,
	I   => 1048, i   => 1080,
	J   => 1049, j   => 1081,
	K   => 1050, k   => 1082,
	L   => 1051, l   => 1083,
	M   => 1052, m   => 1084,
	N   => 1053, n   => 1085,
	O   => 1054, o   => 1086,
	P   => 1055, p   => 1087,
	R   => 1056, r   => 1088,
	S   => 1057, s   => 1089,
	T   => 1058, t   => 1090,
	U   => 1059, u   => 1091,
	F   => 1060, f   => 1092,
	vCH => 1061, vch => 1093,
	Z   => 1062, z   => 1094,
	vTS => 1063, vts => 1095,
	vSC => 1064, vsc => 1096,
	vSH => 1065, vsh => 1097,
	vS1 => 1066, vs1 => 1098,
	Y   => 1067, y   => 1099,
	vS2 => 1068, vs2 => 1100,
	vAE => 1069, vae => 1101,
	vJU => 1070, vju => 1102,
	vJA => 1071, vja => 1103
	};

	return $map;
}
